package cn.com.wxd.dao.website;

import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import cn.com.wxd.dao.HibernateDao;
import cn.com.wxd.entity.website.CommentInfo;
import cn.com.wxd.common.CharUtil;
import cn.com.wxd.common.PageInfo;
import cn.com.wxd.common.data.KeyValue;

/**
 * Title:CommentDaoImpl.java
 * Description:cn.com.wxd.dao.website
 * 
 * Company: wxd网站系统
 * Copyright: Copyright (c) 2015
 * All right reserved.
 * Created time: 2016-6-20 下午2:15:31
 *
 * @author WangXuDong
 * @version 1.0
 */
public class CommentDaoImpl extends HibernateDao implements CommentDao {
	private static final Logger log = LoggerFactory.getLogger(CommentDaoImpl.class);
	
	@Override
	public CommentInfo selectCommentById(int id) {
		Session session = currentSession();
		return (CommentInfo) session.get(CommentInfo.class, id);
	}

	@Override
	public List<CommentInfo> selectCommentByReid(int reid, PageInfo page,KeyValue<String,String> orderKV) {
		Session session = currentSession();
		Criteria c = session.createCriteria(CommentInfo.class);
		Criteria c2 = session.createCriteria(CommentInfo.class);
		c2.add(Restrictions.eq("replyId", reid));
		c.add(Restrictions.eq("replyId", reid));
		int totcount = CharUtil.stringParsInt(
				c2.setProjection(Projections.rowCount())
				.uniqueResult()
				.toString());
		if(orderKV!=null&&orderKV.getValue().equalsIgnoreCase("desc")){
			c.addOrder(Order.desc(orderKV.getKey()));
		}
		if(orderKV!=null&&orderKV.getValue().equalsIgnoreCase("asc")){
			c.addOrder(Order.asc(orderKV.getKey()));
		}
		if(page==null){
			List<CommentInfo> list = (List<CommentInfo>)c.list();
			return list;
		}
		page.setTotalCount(totcount);
		c.setFirstResult((page.getPageIndex()-1)*page.getPageSize());
		c.setMaxResults(page.getPageSize());
		List<CommentInfo> list = (List<CommentInfo>)c.list();
		return list;
	}
	@Override
	public List<CommentInfo> selectRootCommentByArtId(int artId, PageInfo page,KeyValue<String,String> orderKV) {
		Session session = currentSession();
		Criteria c = session.createCriteria(CommentInfo.class);
		Criteria c2 = session.createCriteria(CommentInfo.class);
		c2.add(Restrictions.eq("artId", artId));
		//或查询
		c2.add(Restrictions.eq("replyId", 0));
		c.add(Restrictions.eq("artId", artId));
		c.add(Restrictions.eq("replyId", 0));
		int totcount = CharUtil.stringParsInt(
				c2.setProjection(Projections.rowCount())
				.uniqueResult()
				.toString());
		if(orderKV!=null&&orderKV.getValue().equalsIgnoreCase("desc")){
			c.addOrder(Order.desc(orderKV.getKey()));
		}
		if(orderKV!=null&&orderKV.getValue().equalsIgnoreCase("asc")){
			c.addOrder(Order.asc(orderKV.getKey()));
		}
		if(page==null){
			List<CommentInfo> list = (List<CommentInfo>)c.list();
			return list;
		}
		page.setTotalCount(totcount);
		c.setFirstResult((page.getPageIndex()-1)*page.getPageSize());
		c.setMaxResults(page.getPageSize());
		List<CommentInfo> list = (List<CommentInfo>)c.list();
		return list;
	}
	@Override
	public List<CommentInfo> selectComment(PageInfo page,
			KeyValue<String, String> orderKV) {
		Session session = currentSession();
		Criteria c = session.createCriteria(CommentInfo.class);
		Criteria c2 = session.createCriteria(CommentInfo.class);
		int totcount = CharUtil.stringParsInt(
				c2.setProjection(Projections.rowCount())
				.uniqueResult()
				.toString());
		if(orderKV!=null&&orderKV.getValue().equalsIgnoreCase("desc")){
			c.addOrder(Order.desc(orderKV.getKey()));
		}
		if(orderKV!=null&&orderKV.getValue().equalsIgnoreCase("asc")){
			c.addOrder(Order.asc(orderKV.getKey()));
		}
		if(page==null){
			List<CommentInfo> list = (List<CommentInfo>)c.list();
			return list;
		}
		page.setTotalCount(totcount);
		c.setFirstResult((page.getPageIndex()-1)*page.getPageSize());
		c.setMaxResults(page.getPageSize());
		List<CommentInfo> list = (List<CommentInfo>)c.list();
		return list;
	}
	@Override
	public boolean insertComment(CommentInfo comment) {
		Session session = currentSession();
		if(comment==null){
			return false;
		}
		comment.setId(0);
		try {
			session.save(comment);
			return true;
		} catch (Exception e) {
			log.error("添加评论失败"+e.getMessage(),e);
		}
		return false;
	}
	@Override
	public boolean updateDisabled(int id, boolean isDisabled) {
		Session session = currentSession();
		CommentInfo comment = (CommentInfo)session.get(CommentInfo.class, id);
		comment.setIsDisabled("Y");
		session.update(comment);
		return false;
	}
	@Override
	public boolean updateComment(int id, HashMap<String, Object> param) {
		Session session = currentSession();
		String par = " ";
		if(param==null||param.size()==0){
			return false;
		}
		for (String key : param.keySet()) {
			par+=key+"='"+param.get(key)+"',";
		}
		par=par.substring(0,par.lastIndexOf(".")-1);
		par+=" ";
		String hql = "UPDATE Comment SET "+par+" WHERE ID="+id;
		int reint = session.createQuery(hql).executeUpdate();
		return reint == 1;
	}
	@Override
	public boolean deleteComment(int id) {
		Session session = currentSession();
		session.delete(session.load(CommentInfo.class, id));
		return true;
	}
	@Override
	public int selectCommCountByArtId(int artId) {
		Session session = currentSession();
		Criteria c = session.createCriteria(CommentInfo.class);
		c.add(Restrictions.eq("artId", artId));
		c.add(Restrictions.eq("replyId", 0));
		c.setProjection(Projections.rowCount());
		return Integer.parseInt(String.valueOf(c.uniqueResult()));
	}
	@Override
	public int selectCommCountByRepId(int repId) {
		Session session = currentSession();
		Criteria c = session.createCriteria(CommentInfo.class);
		c.add(Restrictions.eq("replyId", repId));
		c.setProjection(Projections.rowCount());
		int count = Integer.parseInt(String.valueOf(c.uniqueResult()));
		return count;
	}
	@Override
	public boolean insertPraise(int[] commIds, int[] addCounts) {
		Session session = currentSession();
		if(commIds.length!=addCounts.length){
			return false;
		}
		for (int i = 0; i < commIds.length; i++) {
			CommentInfo c = (CommentInfo) session.get(CommentInfo.class, commIds[i]);
			c.setPraise(c.getPraise()+addCounts[i]);
			session.update(c);
		}
		return true;
	}
	@Override
	public boolean insertDislike(int[] commIds, int[] addCounts) {
		Session session = currentSession();
		if(commIds.length!=addCounts.length){
			return false;
		}
		for (int i = 0; i < commIds.length; i++) {
			CommentInfo c = (CommentInfo) session.get(CommentInfo.class, commIds[i]);
			c.setDislike(c.getDislike()+addCounts[i]);
			session.update(c);
		}
		return true;
	}
	@Override
	public boolean insertReport(int[] commIds, int[] addCounts) {
		Session session = currentSession();
		if(commIds.length!=addCounts.length){
			return false;
		}
		for (int i = 0; i < commIds.length; i++) {
			CommentInfo c = (CommentInfo) session.get(CommentInfo.class, commIds[i]);
			c.setReport(c.getReport()+addCounts[i]);
			session.update(c);
		}
		return true;
	}
	@Override
	public List<CommentInfo> selectRootCommentByUserId(int userId,
			PageInfo page, KeyValue<String, String> orderKV) {
		Session session = currentSession();
		Criteria c = session.createCriteria(CommentInfo.class);
		Criteria c2 = session.createCriteria(CommentInfo.class);
		c2.add(Restrictions.eq("userId", userId));
		//或查询
		c2.add(Restrictions.eq("replyId", 0));
		c.add(Restrictions.eq("userId", userId));
		c.add(Restrictions.eq("replyId", 0));
		int totcount = CharUtil.stringParsInt(String.valueOf(
				c2.setProjection(Projections.rowCount())
				.uniqueResult()));
		if(orderKV!=null&&orderKV.getValue().equalsIgnoreCase("desc")){
			c.addOrder(Order.desc(orderKV.getKey()));
		}
		if(orderKV!=null&&orderKV.getValue().equalsIgnoreCase("asc")){
			c.addOrder(Order.asc(orderKV.getKey()));
		}
		if(page==null){
			List<CommentInfo> list = (List<CommentInfo>)c.list();
			return list;
		}
		page.setTotalCount(totcount);
		c.setFirstResult((page.getPageIndex()-1)*page.getPageSize());
		c.setMaxResults(page.getPageSize());
		List<CommentInfo> list = (List<CommentInfo>)c.list();
		return list;
	}
}
